1
The Power of Introspection
AI035 Lesson 15
00:00

In Ruby, code isn't just a static script—it is a living system. Introspection is the superpower that allows objects to possess 'self-knowledge,' transforming them from black boxes into searchable maps of functionality. Even Immediate values like integers can be interrogated about their lineage and capabilities at runtime.

1. The Living Interface

By calling obj.methods(regular=true), we access an object's complete functional vocabulary. A simple Range object (r = 1..10) reveals a staggering 68 distinct methods, reflecting its rich inheritance from Object and the Enumerable mixin.

Sidebar [A]ArrayAtan2AutoLoadClass Array < ObjectEnumerableMethod Signature: Array#packInput: template stringOutput: Binary String

2. Dynamic Discovery

The respond_to? method acts as a runtime gatekeeper, enabling Duck Typing. Instead of asking 'What are you?', we ask 'Can you do this?'. This flexibility allows programs to adapt based on actual capabilities, such as checking if a string responds to assignment-like symbols (=) or if a numeric value supports a specific Hierarchy Query.

3. The Meta-Documentation Bridge

Tools like RDoc use these features to generate Alphabetical listing layouts and method signature block diagrams automatically. By querying Module constants and Instance Variables, developers can navigate the entire namespace structure programmatically, bridging the gap between source files and active execution.

main.py
TERMINAL bash — 80x24
> Ready. Click "Run" to execute.
>